package com.guodu.filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.guodu.entity.UserInfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInterceptor extends AbstractInterceptor
{
  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

public String intercept(ActionInvocation invocation)
    throws Exception
  {
    ActionContext context = invocation.getInvocationContext();
    HttpServletRequest request = (HttpServletRequest)context.get("com.opensymphony.xwork2.dispatcher.HttpServletRequest");
    HttpServletResponse response = (HttpServletResponse)context.get("com.opensymphony.xwork2.dispatcher.HttpServletResponse");
    HttpSession session = request.getSession();
    String path = request.getRequestURI();
   
    UserInfo user = (UserInfo)session.getAttribute("userbean");
    String world = request.getContextPath();
    if (user == null) { 
        if(!path.equals(world+"/UserInfo/login.action")){
        	 response.sendRedirect(world + "/login.jsp?result=1");
        	 return null;
    	}
    }
    return invocation.invoke();
  }

}
